<?php
namespace Home\Controller;
use Think\Controller;
/**
 * 活动控制器
 */
class ActivityController extends Controller{
/**
 * 活动轮播
 */
public  function carousel(){
    $where['devicecode']=I('get.device','','strip_tags');

    $map['deviceid']=M('device')->where($where)->getField("deviceid");////通过设备号查找设备id

    if(empty($map)){
     $re['status']=0;
     $re['msg']="无效的设备";
     echo json_encode($re,JSON_UNESCAPED_UNICODE);
     return '';
   }
    $activity=M('activity_device')->where($map)->distinct(true)->getField("activityid",true);////获取所有的父活动
    if(empty($activity))
    {
     $res['status']=0;
     $res['msg']="该设备没参与活动";
     echo json_encode($res,JSON_UNESCAPED_UNICODE);
     return '';
   }
   $activity=implode(",",array_values($activity));
   $acty['pid']=array("in",$activity);
   $acty['state']=1;///已经上线的活动
   $carousel=M('activity')->where($acty)->Field("pid,id,childimg,opentime,endtime,childtheme")->select();////轮播子活动图
  if(empty($carousel))
  {
     $re['status']=0;
     $re['msg']='暂无数据';
     echo json_encode($re,JSON_UNESCAPED_UNICODE);
   }else{
        $result['status']=1;
        $result['tree']=$carousel;
        echo json_encode($result,JSON_UNESCAPED_UNICODE);
      } 

    }
/**
 * 主题页面接口
 */
public function theme(){
  $where['id']=I('get.pid',0,'strip_tags');
  $parentimg=M('activity')->where($where)->getField("parentimg");
  if(empty($parentimg)){
    $re['status']=0;
    $re['msg']='暂无数据';
    echo json_encode($re,JSON_UNESCAPED_UNICODE);
  }else{
    $result['status']=1;
    $result['img']=$parentimg;
    echo json_encode($result,JSON_UNESCAPED_UNICODE);
  }
}
/**
 * 活动时间
 */
public function activitytime(){
  $id=I('get.id',0,'strip_tags');

  if(empty($id)){
    $re['status']=0;
    $re['msg']='无效的参数';
    echo json_encode($re,JSON_UNESCAPED_UNICODE);
    return '';

  }
  $res=M('activity')->where("id='%d'",$id)->Field("opentime,endtime")->select();
  foreach ($res as $key => $value)
   {
    $res[$key]['time']=time();
  }
  if(empty($res))
  {
     $ret['status']=2;
    $ret['msg']='该活动不存在';
    echo json_encode($ret,JSON_UNESCAPED_UNICODE);
    return '';
  }

  $result['status']=1;
  $result['msg']='成功';
  $result['act']=$res[0];
  echo json_encode($result,JSON_UNESCAPED_UNICODE);

}


}